using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using BattleCity.GameLogic;



namespace BattleCity.Rendering
{
    public class HudRenderer
    {
        Texture2D enemiesHudTex;
        Texture2D playerHudTex;
        Texture2D levelHudTex;
        Texture2D gameOverBanner;
        Texture2D levelCompleteBanner;
        Texture2D numbersBanner;
        Texture2D bannerWrapper;

        Vector2 enemiesHudPos;
        Vector2 playerHudPos;
        Vector2 levelHudPos;
       // Vector2 levelStringPos;

        //Font
        SpriteFont hudFont;

        Rectangle[] levelRectangles;
        //Rectangle[] numbersRectangles;
        Rectangle firstNumberRectangle;
        Rectangle secondNumberRectangle;

        int screenWidth;

        public HudRenderer(ContentManager content, GraphicsDevice graphicsDevice, int screenWidth)
        {
            hudFont = content.Load<SpriteFont>("menu");

            this.screenWidth = screenWidth;

            enemiesHudTex = content.Load<Texture2D>(@"hud_textures\enemies_hud");
            playerHudTex = content.Load<Texture2D>(@"hud_textures\player_hud");
            levelHudTex = content.Load<Texture2D>(@"hud_textures\level_hud");
            gameOverBanner = content.Load<Texture2D>(@"hud_textures\game_over");
            levelCompleteBanner = content.Load<Texture2D>(@"hud_textures\level_complete");
            numbersBanner = content.Load<Texture2D>(@"hud_textures\numbers");

            Color[] bannerWrapperColorData = new Color[60 * screenWidth];
            for (int i = 0; i < 60; ++i)
            {
                for (int j = 0; j < screenWidth; ++j)
                {
                    if ((i == 59) || (i == 0))
                        bannerWrapperColorData[i * screenWidth + j] = new Color(15, 115, 136, 250);
                    else
                        bannerWrapperColorData[i * screenWidth + j] = new Color(0, 0, 0, 200);
                }
            }

            bannerWrapper = new Texture2D(graphicsDevice, screenWidth, 60);
            bannerWrapper.SetData<Color>(bannerWrapperColorData);

            enemiesHudPos = new Vector2(20, 10);
            playerHudPos = new Vector2(screenWidth - playerHudTex.Width - 20, 10);
            levelHudPos = new Vector2((screenWidth * 0.5f - levelHudTex.Width * 0.5f), -22.0f);

            levelRectangles = new Rectangle[2];
            levelRectangles[0] = new Rectangle((int)(screenWidth * 0.5f - levelCompleteBanner.Width * 0.35f), (int)(244 - levelCompleteBanner.Height * 0.5f), 300, 60);
            levelRectangles[1] = new Rectangle(0, 0, 300, 60);

            firstNumberRectangle = new Rectangle((int)(screenWidth * 0.5f + 100), (int)(244 - levelCompleteBanner.Height * 0.5f), 80, 55);
            secondNumberRectangle = new Rectangle((int)(screenWidth * 0.5f + 150), (int)(244 - levelCompleteBanner.Height * 0.5f), 80, 55);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            //GAME HUD RENDERING
            spriteBatch.Begin();
            spriteBatch.Draw(enemiesHudTex, enemiesHudPos, Color.White);
            spriteBatch.Draw(playerHudTex, playerHudPos, Color.White);
            //spriteBatch.Draw(levelHudTex, levelHudPos, Color.White);

            string number = (20 - GameState.gameState.DestroyedEnemies.Value).ToString();
            if ((20 - GameState.gameState.DestroyedEnemies.Value) < 10) number = "0" + number;

            spriteBatch.DrawString(hudFont, number, new Vector2(105, 15), Color.Black);
            spriteBatch.DrawString(hudFont, number, new Vector2(103, 13), Color.Red);

            number = GameState.gameState.PlayerLives.Value.ToString();
            if (GameState.gameState.PlayerLives.Value < 10) number = "0" + number;
            spriteBatch.DrawString(hudFont, number, new Vector2(playerHudPos.X + 15, 15), Color.Black);
            spriteBatch.DrawString(hudFont, number, new Vector2(playerHudPos.X + 13, 13), Color.Green);


            if (GameState.bannerOnScreen.Value)
            {
                if (GameState.gameState.PlayerLives.Value == 0)
                {
                    spriteBatch.Draw(bannerWrapper, new Vector2(0.0f, 242 - bannerWrapper.Height * 0.5f), Color.White);
                    spriteBatch.Draw(gameOverBanner, new Vector2(screenWidth * 0.5f - gameOverBanner.Width * 0.5f, 240 - gameOverBanner.Height * 0.5f), Color.White);
                }
                else if (GameState.gameState.DestroyedEnemies.Value == 20)
                {
                    spriteBatch.Draw(bannerWrapper, new Vector2(0.0f, 242 - bannerWrapper.Height * 0.5f), Color.White);
                    spriteBatch.Draw(levelCompleteBanner, new Vector2(screenWidth * 0.5f - levelCompleteBanner.Width * 0.5f, 240 - levelCompleteBanner.Height * 0.5f), Color.White);
                }
                else
                {
                    spriteBatch.Draw(bannerWrapper, new Vector2(0.0f, 242 - bannerWrapper.Height * 0.5f), Color.White);
                    spriteBatch.Draw(levelCompleteBanner, levelRectangles[0], levelRectangles[1], Color.White);
                    spriteBatch.Draw(numbersBanner, firstNumberRectangle, selectNumberRectangle(GameState.levelNumber.Value / 10), Color.White);
                    spriteBatch.Draw(numbersBanner, secondNumberRectangle, selectNumberRectangle(GameState.levelNumber.Value % 10), Color.White);
                }
            }

            spriteBatch.End();
        }

        public Rectangle selectNumberRectangle(int number)
        {
            switch (number)
            {
                case 1:
                    return new Rectangle(10, 0, 80, 60);
                case 2:
                    return new Rectangle(80, 0, 80, 60);
                case 3:
                    return new Rectangle(160, 0, 80, 60);
                case 4:
                    return new Rectangle(240, 0, 80, 60);
                case 5:
                    return new Rectangle(320, 0, 80, 60);
                case 6:
                    return new Rectangle(10, 95, 80, 60);
                case 7:
                    return new Rectangle(90, 95, 80, 60);
                case 8:
                    return new Rectangle(150, 95, 80, 60);
                case 9:
                    return new Rectangle(250, 95, 80, 60);
                default:
                    return new Rectangle(330, 95, 80, 60);
            }

        }
    }

}
